<template>
	<view class="chat">
		<view class="chat_main">
			<scroll-view :refresher-triggered="refreshStatus" @refresherrefresh="refresherrefresh"
				:refresher-enabled="status == 'loadmore'" :scroll-into-view="scrollId" scroll-y="true" class="scroll-Y">
				<view class="chat_main_item" v-for="(item,index) in list" :key="index" :id="`x${item.id}`">
					<template v-if="item.sender.id == myId">
						<view class="chat_main_myItem">
							<view class="chat_main_myItem_top">
								{{timestamp(item.createtime)}}
							</view>
							<view class="chat_main_myItem_bottom">
								<view class="chat_main_myItem_bottom_left">
									<view class="chat_main_myItem_bottom_left_icon">
										<u-icon name="play-right-fill" color="#88ee5e" size="22"></u-icon>
									</view>
									<view class="chat_main_myItem_bottom_left_main">
										{{item.content}}
									</view>
								</view>
								<view class="chat_main_myItem_bottom_right">
									<view class="chat_main_myItem_bottom_right_ava">
										<image :src="item.sender.avatar" mode=""></image>
									</view>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view class="chat_main_myItem" style="align-items: flex-start;">
							<view class="chat_main_myItem_top">
								{{timestamp(item.createtime)}}
							</view>
							<view class="chat_main_myItem_bottom">
								<view class="chat_main_myItem_bottom_right" style="margin-right: 20rpx;margin-left: 0;">
									<view class="chat_main_myItem_bottom_right_ava">
										<image :src="item.sender.avatar" mode=""></image>
									</view>
								</view>
								<view class="chat_main_myItem_bottom_left" style="margin-left: 0;">
									<view class="chat_main_myItem_bottom_left_icon" style="left: -14rpx;">
										<u-icon name="play-left-fill" color="#88ee5e" size="22"></u-icon>
									</view>
									<view class="chat_main_myItem_bottom_left_main">
										{{item.content}}
									</view>
								</view>
							</view>
						</view>
					</template>
				</view>
			</scroll-view>
		</view>
		<view class="chat_footer">
			<view class="chat_footer_left">
				<u-input :clearable="false" :border="true" height="60" placeholder="输入内容点击发送" v-model="msg"
					:custom-style="{
						width:'500rpx'
					}"></u-input>
			</view>
			<view class="chat_footer_right" @click="goSend">
				发送
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getChatHistory,
		getChatSend
	} from '@/api/index.js'
	export default {
		data() {
			return {
				session_id: '',
				page: 1,
				session_name: '',
				msg: '',
				list: [],
				scrollId: '',
				refreshStatus: false,
				status: 'loadmore',
				myId: ''
			}
		},
		methods: {
			async refresherrefresh() {
				this.refreshStatus = true
				if (this.status == 'nomore') {
					this.refreshStatus = false
					return uni.showToast({
						icon: 'none',
						title: '没有更多了'
					})
				}
				setTimeout(async () => {
					this.page++
					await this.getHistory(false)
					this.refreshStatus = false
				}, 1000)
			},
			timestamp(timestamp) {
				var date = new Date(timestamp * 1000); // 转换为毫秒
				var year = date.getFullYear();
				var month = ('0' + (date.getMonth() + 1)).slice(-2);
				var day = ('0' + date.getDate()).slice(-2);
				var hours = ('0' + date.getHours()).slice(-2);
				var minutes = ('0' + date.getMinutes()).slice(-2);
				var seconds = ('0' + date.getSeconds()).slice(-2);

				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			async goSend() {
				if (this.msg === '') {
					return uni.showToast({
						icon: 'none',
						title: '不能发送空消息'
					})
				}
				let data = {
					session_id: this.session_id,
					message: this.msg
				}
				await getChatSend(data)
				this.msg = ''
				await this.getHistory(true)
			},
			async getHistory(isAdd) {
				let data = {
					session_id: this.session_id,
					page: isAdd ? 1 : this.page,
					limit: isAdd ? 1 : 10,
					order: 'desc'
				}
				const res = await getChatHistory(data)
				if (isAdd) {
					this.list = [...this.list, ...(res.data.data).reverse()]
				} else {
					this.list = [...(res.data.data.reverse()), ...this.list]
				}
				if (res.data.data.length > 0) {
					this.$nextTick(() => {
						this.scrollId = 'x' + res.data.data[res.data.data.length - 1].id
					})
				}
				if (!isAdd && res.data.data.length < 10) {
					this.status = 'nomore'
				} else {
					this.stauts = 'loadmore'
				}
			}
		},
		async onLoad(e) {
			this.myId = uni.getStorageSync('userInfo').id
			if (!e || !e.chat_id) {
				return uni.showToast({
					icon: 'none',
					title: '参数缺失'
				})
			}
			this.session_id = e.chat_id
			this.session_name = e.name
			await this.getHistory(false)
		},
		onReady() {
			if (this.session_name) {
				uni.setNavigationBarTitle({
					title: this.session_name
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.chat {
		height: 100vh;
		background-color: #ededed;
		// padding: 88rpx 0 80rpx 0;
		padding-bottom: 80rpx;

		.chat_main {
			width: 100%;
			height: 100%;
			padding: 20rpx;

			.scroll-Y {
				height: 100%;

				.chat_main_item {
					margin-bottom: 30rpx;
				}

				.chat_main_myItem {
					display: flex;
					flex-direction: column;
					align-items: flex-end;

					.chat_main_myItem_top {
						color: #999;
						font-size: 22rpx;
					}

					.chat_main_myItem_bottom {
						margin-top: 10rpx;
						display: flex;

						.chat_main_myItem_bottom_left {
							border-radius: 8rpx;
							padding: 18rpx 20rpx;
							background-color: #88ee5e;
							color: #333;
							line-height: 32rpx;
							font-size: 26rpx;
							height: fit-content;
							position: relative;

							.chat_main_myItem_bottom_left_main {
								max-width: 530rpx;
								white-space: normal;
								word-break: break-word;
							}

							.chat_main_myItem_bottom_left_icon {
								position: absolute;
								right: -14rpx;
								top: 14rpx;
								width: fit-content;
							}
						}

						.chat_main_myItem_bottom_right {
							margin-left: 20rpx;

							.chat_main_myItem_bottom_right_ava {
								width: 70rpx;
								height: 70rpx;

								image {
									width: 100%;
									height: 100%;
									border-radius: 6rpx;
								}
							}
						}
					}
				}
			}
		}

		.chat_footer {
			height: 80rpx;
			background-color: #f7f7f7;
			position: fixed;
			bottom: 0;
			width: 100%;
			left: 0;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 20rpx 0 30rpx;

			.chat_footer_left {}

			.chat_footer_right {
				width: 120rpx;
				height: 60rpx;
				text-align: center;
				line-height: 60rpx;
				font-size: 28rpx;
				color: #fff;
				background-color: #00c65c;
				border-radius: 10rpx;
			}
		}
	}
</style>